﻿/**
*
*  RSSReader is a lightweight RSS client written in C#
*
*  Copyright 2012 Ashot Aslanyan <ashot.aslanian@gmail.com>
*
*  This file is part of RSSReader.
*
*  RSSReader is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  RSSReader is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with servmonitor.  If not, see <http://www.gnu.org/licenses/>.
*
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace RSSReader
{
    /// <summary>
    /// Entry point of application
    /// </summary>
    /// <remarks>
    /// <para>
    /// Entry point of application. It is calling <c>ShowView</c>
    /// method of <c>IView</c> interface and shows application.
    /// </para>
    /// </remarks>
    class RSSReaderApplication
    {
        /// <summary>
        /// Config file directory
        /// </summary>
        public static string AppDataPath
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + "RSSReader" + Path.DirectorySeparatorChar;
            }
        }

        /// <summary>
        /// <c>Main</c> function
        /// </summary>
        /// <param name="args">command line args</param>
        [STAThread]
        public static void Main(string[] args)
        {
            bool debug = false;
            bool file = false;

            foreach (string opt in args)
            {
                switch (opt)
                {
                    case "-d":
                    case "--debug":
                        debug = true;
                    break;

                    case "-f":
                    case "--infile":
                        file = true;
                    break;

                    case "-df":
                    case "-fd":
                        file = true;
                        debug = true;
                    break;
                    case "-h":
                    case "--help":
                        RSSReaderApplication.Usage();
                        Environment.Exit(0);
                    break;
                    default:
                        RSSReaderApplication.Usage();
                        Environment.Exit(1);
                    break;
                }
            }

            Logger logger = Logger.GetInstance("RssReaderApplication", file, debug);
            IView view = new FormView();

            view.ShowView();

            Environment.Exit(0);
        }

        /// <summary>
        /// Reads configuration file and get URLs of feeds.
        /// </summary>
        /// <param name="feedURLs">A <see cref="System.Collections.Generic.List{T}"/> where to store URLs</param>
        /// <returns></returns>
        /// <exception cref="Exception">
        /// If cannot open the existing configuration file.
        /// </exception>
        public static void GetFeedURLs(ref List<string> feedURLs)
        {
            Logger.Instance.Log("RSSReaderApplication", String.Format("config dir {0}. config file {1}", RSSReaderApplication.AppDataPath, "rssreader.conf"));
            if (Directory.Exists(RSSReaderApplication.AppDataPath))
            {
                try
                {
                    using (StreamReader r = new StreamReader(RSSReaderApplication.AppDataPath + "rssreader.conf"))
                    {
                        string line;
                        while ((line = r.ReadLine()) != null)
                            feedURLs.Add(line);
                    }
                }
                catch (FileNotFoundException)
                {
                    Logger.Instance.Log("RssReaderApplication", "no config file found. creating...");
                    try
                    {
                        File.Create(RSSReaderApplication.AppDataPath + "rssreader.conf");
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.Log("RssReaderApplication", "could not create config file.");
                        throw(new CreateConfigFileException("could not create config file", ex));
                    }
                    Logger.Instance.Log("RssReaderApplication", "created");
                }
            }
            else
            {
                Logger.Instance.Log("RssReaderApplication", "directory not found. creating...");
                try
                {
                    Directory.CreateDirectory(RSSReaderApplication.AppDataPath);
                }
                catch (Exception ex)
                {
                    Logger.Instance.Log("RssReaderApplication", "could not create config directory.");
                    throw (new CreateConfigDirException("could not create config directory", ex));
                }
                Logger.Instance.Log("RssReaderApplication", "created");

                Logger.Instance.Log("RssReaderApplication", "creating config file...");
                try
                {
                    File.Create(RSSReaderApplication.AppDataPath + "rssreader.conf");
                }
                catch (Exception ex)
                {
                    Logger.Instance.Log("RssReaderApplication", "could not create config file.");
                    throw (new CreateConfigFileException("could not create config file", ex));
                }
                Logger.Instance.Log("RssReaderApplication", "created");
            }
        }

        /// <summary>
        /// Display help message
        /// </summary>
        private static void Usage()
        {
            string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            Console.WriteLine("RSSReader v {0}", version);
            Console.WriteLine("Copyright (c) 2012 Ashot Aslanyan <ashot.aslanian@gmail.com>");
            Console.WriteLine("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>");
            Console.WriteLine("This is free software: you are free to change and redistribute it.");
            Console.WriteLine("There is NO WARRANTY, to the extent permitted by law.");
            Console.WriteLine();

            Console.WriteLine("-h | --help   \t  Displays this message");
            Console.WriteLine("-d | --debug  \t  Turn debugging on");
            Console.WriteLine("-i | --infile \t  Log to file <log.txt in programs directory>");
        }
    }
}